﻿Imports WMPLib
Imports System.IO

Public Class Form1
    Dim Playlist As IWMPPlaylist = Nothing
    Dim unsaved As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\" & "unsaved.m3u"

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If File.Exists(My.Settings.Lastlist) Then
            Playlist = Player.newPlaylist(Path.GetFileNameWithoutExtension(My.Settings.Lastlist), My.Settings.Lastlist)
            Player.currentPlaylist = Playlist
            For i = 0 To Player.currentPlaylist.count - 1
                ListBox1.Items.Add(Player.currentPlaylist.Item(i).name)
            Next
        End If
    End Sub

    Private Sub OpenFileToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenFileToolStripMenuItem.Click
        Dim ofd As New OpenFileDialog
        ofd.Multiselect = True
        ofd.RestoreDirectory = True
        ofd.Filter = "Music File|*.mp3"

        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            ListBox1.Items.Clear()
            Using sw As New StreamWriter(unsaved)
                For Each song As String In ofd.FileNames
                    sw.WriteLine(song)
                Next
            End Using
            Playlist = Player.newPlaylist("Unsaved", unsaved)
            Player.currentPlaylist = Playlist

            ' Add track's on playlist
            For i = 0 To Player.currentPlaylist.count - 1
                ListBox1.Items.Add(Player.currentPlaylist.Item(i).name)
            Next

            My.Settings.Lastlist = unsaved
        End If

    End Sub

    Private Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
        If ListBox1.SelectedIndex <> -1 Then
            Player.Ctlcontrols.playItem(Player.currentPlaylist.Item(ListBox1.Items.IndexOf(ListBox1.SelectedItem)))
        End If
    End Sub

    Private Sub Player_CurrentItemChange(sender As Object, e As AxWMPLib._WMPOCXEvents_CurrentItemChangeEvent) Handles Player.CurrentItemChange
        If ListBox1.Items.Contains(Player.Ctlcontrols.currentItem.name) Then
            ListBox1.SelectedItem = Player.Ctlcontrols.currentItem.name
        End If
    End Sub

    Private Sub AddTracksToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles AddTracksToolStripMenuItem1.Click
        Dim ofd As New OpenFileDialog
        ofd.Multiselect = True
        ofd.RestoreDirectory = True
        ofd.Filter = "Music File|*.mp3"
        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            ListBox1.Items.Clear()
            For Each song As String In ofd.FileNames
                Player.currentPlaylist.appendItem(Player.newMedia(song))
            Next

            Using sw As New StreamWriter(My.Settings.Lastlist, False)
                For i = 0 To Player.currentPlaylist.count - 1
                    ListBox1.Items.Add(Player.currentPlaylist.Item(i).name)
                    sw.WriteLine(Player.currentPlaylist.Item(i).sourceURL)
                Next
            End Using
        End If
    End Sub

    Private Sub RemoveTracksToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles RemoveTracksToolStripMenuItem1.Click
        Player.currentPlaylist.removeItem(Player.currentPlaylist.Item(ListBox1.Items.IndexOf(ListBox1.SelectedItem)))
        ListBox1.Items.Remove(ListBox1.SelectedItem)
        ListBox1.Items.Clear()
        Using sw As New StreamWriter(My.Settings.Lastlist, False)
            For i = 0 To Player.currentPlaylist.count - 1
                ListBox1.Items.Add(Player.currentPlaylist.Item(i).name)
                sw.WriteLine(Player.currentPlaylist.Item(i).sourceURL)
            Next
        End Using
    End Sub


    Private Sub AboutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem.Click
        MessageBox.Show("Udroiu Catalin Ionut (c) UCI - 2015")
    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub
End Class
